package com.wfm.leetcode.editor.cn;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class POne28最长连续序列{
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int longestConsecutive(int[] nums) {
        Set<Integer> s = new HashSet<>();
        for (int num:nums){
            s.add(num);
        }
        int ans = 0;
        for (int num:s){
            if(!s.contains(num-1)){
                int curNum = num;
                int curLong = 1;
                while (s.contains(curNum+1)){
                    curNum=curNum+1;
                    curLong+=1;
                }
                ans = Math.max(curLong,ans);
            }
        }
        return ans;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) { 
        Solution solution = new POne28最长连续序列().new Solution();
        int i = solution.longestConsecutive(new int[]{9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6});
        System.out.println(i);
    }
}